using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Deepcode.Flux.Core.Data;
using Deepcode.Flux.Core.Systems.Workflow.Enums;
using Deepcode.Flux.Core.Systems.Workflow.Info;

namespace Deepcode.Flux.Core.Systems.Workflow.Bases
{
	/// <summary>
	/// Represents an exit path for a tasking
	/// </summary>
	public class TaskExitPath
	{
		// Private fields for storing record fields //
		private int					_PK_ID = -1;
		private int					_FK_TaskingID = -1;
		private string				_Caption = "";
		private string				_uiTaskMethod = "";
		private string				_CSV_AllowedGroups = "";

		// Public fields exposing record fields //
		public int PK_ID { get { return _PK_ID; } set { _PK_ID = value; } }
		public int FK_TaskingID { get { return _FK_TaskingID; } set { _FK_TaskingID = value; } }
		public string Caption { get { return _Caption; } set { _Caption = value; } }
		public string uiTaskMethod { get { return _uiTaskMethod; } set { _uiTaskMethod = value; } }
		public string CSV_AllowedGroups { get { return _CSV_AllowedGroups; } set { _CSV_AllowedGroups = value; } }

		// Calced fields for data binding
		public string CalcUITaskMethod
		{
			get
			{
				if (this.uiTaskMethod == "") return "(none)";

				string HandlerID = this.ParentTasking.ParentProcess.ParentDefinition.ParentHandler.ID;
				WorkflowEngine engine = new WorkflowEngine(HandlerID);
				WorkflowUIMethodInfo inf = engine.GetWorkflowUIMethodInfo(uiTaskMethod);
				
				return inf.FriendlyName;
			}
		}

		public string CalcCSV_AllowedGroups
		{
			get
			{
				if (this.CSV_AllowedGroups == "") return "Everyone";

				// Get an engine
				string HandlerID = this.ParentTasking.ParentProcess.ParentDefinition.ParentHandler.ID;
				WorkflowEngine engine = new WorkflowEngine(HandlerID);

				string result = "";
				string[] IDList = this.CSV_AllowedGroups.Split(',');

				foreach (string StrID in IDList)
				{
					int ID = -1;
					try { ID = Int32.Parse(StrID); }
					catch { ID = -1; }

					if (ID != -1)
					{
						WorkflowTaskableGroup grp = engine.GetTaskableGroup(ID);
						if (grp != null)
						{
							if (result != "") result += ", ";
							result += grp.GroupName;
						}
					}
				}

				if (result == "") return "Everyone";
				return result;
			}
		}


		// Link backs
		public Tasking ParentTasking { get { return new Taskings().GetTaskingByID(this.FK_TaskingID); } }
		
		// Constructor to create new without loading
		public TaskExitPath () { }

		// Constructor loading from a data row
		public TaskExitPath(DataRow InitFromRow)
		{
			_PK_ID = SQLUtility.DBNull(InitFromRow["PK_ID"], -1);
			_FK_TaskingID = SQLUtility.DBNull(InitFromRow["FK_TaskingID"], -1 );
			_Caption = SQLUtility.DBNull(InitFromRow["Caption"], "" );
			_uiTaskMethod = SQLUtility.DBNull(InitFromRow["uiTaskMethod"], "");
			_CSV_AllowedGroups = SQLUtility.DBNull(InitFromRow["CSV_AllowedGroups"], "");
		}

		/// <summary>
		/// Adds an action to this exit path
		/// </summary>
		/// <param name="Method"></param>
		public void AddAction(string Method)
		{
			if (this.PK_ID == -1) throw new Exception("Cannot add action to an unsaved exit path");

			ExitActions eas = new ExitActions();
			ExitAction ea = new ExitAction();
			ea.FK_ExitPathID = this.PK_ID;
			ea.Rank = eas.GetNextRank( this.PK_ID );
			ea.Method = Method;
			ea.PropertyBag = "";

			eas.Save( ea );
		}
	}
}
